package ${packagePath};

import java.util.concurrent.*;

public class ThreadUtil {

    private static final BlockingQueue blockingQueue = new ArrayBlockingQueue (50);

    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor (20,30,30, TimeUnit.SECONDS,blockingQueue);

    static {
        threadPoolExecutor.allowCoreThreadTimeOut(true);
    }


    public static void run(Runnable runnable){
        threadPoolExecutor.execute(runnable);
    }

    public static <V> V call(Callable<V> callable) throws InterruptedException, ExecutionException, TimeoutException{
        return call(callable,null,null);
    }

    public static <V> V call(Callable<V> callable,Long longtime,TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException{
        Future<V> submit = threadPoolExecutor.submit(callable);
        if(longtime == null || timeUnit == null){
            return submit.get();
        }
        return submit.get(longtime,timeUnit);
    }

}